const fs = require('fs');
const RenderStream = require('./render_stream');
const path = require('path');

class Renderer {
  /**
   * 
   * @param {String} from   模板文件目录
   * @param {String} to     新建工程目录
   * @param {Object} config 模板渲染参数
   */
  constructor (from, to, config) {
    this._from = from;
    this._to = to;
    this._config = config;
  }

  /**
   * 渲染模板文件
   * @param {String} path 模板文件路径
   */
  render (tmpPath) {
    let to = path.join(this._to, path.relative(this._from, tmpPath));
    if (tmpPath.endsWith('.mustache')) {
      to = path.parse(to);
      fs.createReadStream(tmpPath)
        .pipe(new RenderStream(this._config))
        .pipe(fs.createWriteStream(path.join(to.dir, to.name)));
    } else {
      fs.copyFile(tmpPath, to, () => {});
    }
  }
}

module.exports = Renderer;